package zozopo.manual.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import zozopo.comm.Encrypt;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;
import zozopo.user.operations.UserTB;

public class ContentOS extends OperatingService{
	
	
	public ContentOS(Connection conn){
		super(conn);
	}
	
	public ContentOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ContentTB> selectListBySql(String sql){
		List<ContentTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);

		if(resultSet != null){
				try {
					list = new ArrayList<ContentTB>();
					while(resultSet.next()){
						ContentTB content = new ContentTB();
						
						content.setId(resultSet.getInt(ContentTB.ID));
						
						content.setBear(resultSet.getInt(ContentTB.BEAR));
						content.setCnbrand(resultSet.getString(ContentTB.CNBRAND));
						content.setContext(resultSet.getString(ContentTB.CONTEXT));
						content.setDate(resultSet.getString(ContentTB.DATE));
						content.setEnbrand(resultSet.getString(ContentTB.ENBRAND));
						content.setImage(resultSet.getString(ContentTB.IMAGE));
						content.setIp(resultSet.getString(ContentTB.IP));
						content.setIsdraft(resultSet.getBoolean(ContentTB.ISDRAFT));
						content.setIsvisable(resultSet.getBoolean(ContentTB.ISVISABLE));
						
						content.setKind(resultSet.getString(ContentTB.KIND));
						content.setReplys(resultSet.getInt(ContentTB.REPLYS));
						content.setTitle(resultSet.getString(ContentTB.TITLE));
						content.setUrl(resultSet.getString(ContentTB.URL));
						content.setUserid(resultSet.getInt(ContentTB.USERID));
						
						content.setUsermail(resultSet.getString(ContentTB.USERMAIL));
						content.setUsername(resultSet.getString(ContentTB.USERNAME));
						content.setViews(resultSet.getInt(ContentTB.VIEWS));
						
						list.add(content);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ContentTB> select(){
		
		String sql = "SELECT * FROM " + ContentTB.TABLE_NAME 
		+ " WHERE " +ContentTB.ISDRAFT + "=false "
		+ " ORDER BY "+ContentTB.ID + " DESC";
		List<ContentTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		list = selectListBySql(sql);
		
		return list;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertDraft(int userid){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(ContentTB.USERID, userid);
		//insertMap.put(ContentTB.USERNAME, username);
		insertMap.put(ContentTB.ISDRAFT, true);
		insertMap.put(ContentTB.ISVISABLE, false);
		
		return sqlMachine.insertRow(ContentTB.TABLE_NAME, insertMap);
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public ContentTB getDraft(int userid){
		if(isExit(userid)){
			String sql ="SELECT * FROM " + ContentTB.TABLE_NAME + " WHERE " + ContentTB.USERID + " = " + userid
			+ " AND " + ContentTB.ISDRAFT + "=true";
			List<ContentTB> list = selectListBySql(sql);
			
			return list.get(0);
		}else{
			if(insertDraft(userid)){
				String sql ="SELECT * FROM " + ContentTB.TABLE_NAME + " WHERE " + ContentTB.USERID + " = " + userid
				+ " AND " + ContentTB.ISDRAFT + "=true";
				List<ContentTB> list = selectListBySql(sql);
				
				return list.get(0);
			}else{
				return null;
			}
		}
		
		
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public ContentTB get(int id){
		
		String sql ="SELECT * FROM " + ContentTB.TABLE_NAME + " WHERE " + ContentTB.ID + " = " + id;
		
		List<ContentTB> list = selectListBySql(sql);
			
		return list.get(0);
	} 
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean drop(int id){   //未做线程安全处理
		boolean isSuccess = false;
		ContentImagesOS contentImagesOS = new ContentImagesOS(conn);
		
		//1.清除图片文件		
		for(int i=1;i<=12;i++){
			contentImagesOS.clear(id, i);
		}
		
		//2.删除数据库数据
		isSuccess = delete(id);
		
		return isSuccess;
	}
	
	
	
	/**
	 * @version 1.0
	 * @author gee
	 */
	
	public boolean isCurrentUser(int id,String username){
		String sql = "select * from "+ContentTB.TABLE_NAME+" where "+ContentTB.ID+"="+id
		+" and "+ContentTB.USERNAME+"='"+username+"'";
		
		boolean eResult = false;
		try {
			ResultSet result = databaseConnection.executeQuery(conn, sql);
			if(result.next()){
				eResult = true;
			}
			result.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return eResult;
	}
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean delete(int id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		return sqlMachine.deleteRow(ContentTB.TABLE_NAME, ContentTB.ID, id);
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean update(ContentTB content){
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(ContentTB.TITLE, content.getTitle());
		updateMap.put(ContentTB.CNBRAND, content.getCnbrand());
		updateMap.put(ContentTB.ENBRAND, content.getEnbrand());
		updateMap.put(ContentTB.KIND, content.getKind());
		updateMap.put(ContentTB.CONTEXT, content.getContext());
		updateMap.put(ContentTB.IMAGE, content.getImage());
		updateMap.put(ContentTB.USERNAME, content.getUsername());
		
		return sqlMachine.updateRow(ContentTB.TABLE_NAME, updateMap, ContentTB.ID,content.getId());
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean updateDraft(int id,boolean b){
		
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		

		updateMap.put(ContentTB.ISDRAFT, b);
		
		return sqlMachine.updateRow(ContentTB.TABLE_NAME, updateMap, ContentTB.ID,id);
	}
	
	

	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isExit(int userid){
		String sql ="SELECT * FROM " + ContentTB.TABLE_NAME + " WHERE " + ContentTB.USERID + " = " + userid
		+ " AND " + ContentTB.ISDRAFT + "=true";
		
		List<ContentTB> list = selectListBySql(sql);
		
		if(list.size() >= 1){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean isExitContent(int id){
		String sql ="SELECT * FROM " + ContentTB.TABLE_NAME + " WHERE " + ContentTB.ID + " = " + id;
		
		List<ContentTB> list = selectListBySql(sql);
		
		if(list.size() >= 1){
			return true;
		}else{
			return false;
		}
		
	}

}
